﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Remoting.Channels;
using System.Collections;
using System.Runtime.Remoting.Channels.Http;
using DrawerGeneral;
using System.Runtime.Remoting;
using System.Windows.Forms;
using Drawer.Controls;
using System.Windows.Media;


namespace ToServer
{
    public class ToServerCommunicator
    {
        private IRemote ToServerObj;
        private String ClientNameString;
        private DrawCanvas drawCanvas;
        public String ServerIP;
        public int ServerPort;

        public IRemote ToServerConn
        {
            get { return ToServerObj; }
            set { ToServerObj = value; }
        }

        public ToServerCommunicator(String ClientNameString, String ServerIP, int ServerPort)
        {
            this.ClientNameString = ClientNameString;
            this.ServerIP = ServerIP;
            this.ServerPort = ServerPort;
            MakeConnectionToServer(ServerIP, ServerPort);
        }

        public ToServerCommunicator(String ClientNameString, DrawCanvas drawCanvas, String ServerIP, int ServerPort)
        {
            this.ClientNameString = ClientNameString;
            this.drawCanvas = drawCanvas;
            this.ServerIP = ServerIP;
            this.ServerPort = ServerPort;
            MakeConnectionToServer(ServerIP, ServerPort);
        }

        public void CanvasChangeHandler(object sender, CanvasChildrenChangeEventArgs e)
        {
            ToServerConn.DispatchCanvasChangedEvent(ClientNameString, e);
        }

        public void CanvasEditHandler(object sender, CanvasChildEditEventArgs e)
        {
            ToServerConn.DispatchCanvasEditedEvent(ClientNameString, e);
        }

        private void MakeConnectionToServer(string ServerIP, int Port)
        {
            try
            {
                BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
                BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
                serverProvider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;

                IDictionary props = new Hashtable();
                props["port"] = 0;
                string s = System.Guid.NewGuid().ToString();
                props["name"] = s;
                props["typeFilterLevel"] = TypeFilterLevel.Full;
                HttpChannel chan = new HttpChannel(props, clientProvider, serverProvider);

                ChannelServices.RegisterChannel(chan, false);

                ToServerObj = (IRemote)Activator.GetObject(typeof(IRemote), "http://" + ServerIP + ":" + Port + "/RemoteObject.soap", WellKnownObjectMode.SingleCall);                
                
                Color c = (Color) ColorConverter.ConvertFromString(ToServerObj.GetFillColorAndCurrentCanvas(ClientNameString).ColorCode);
                this.drawCanvas.Fill = (Brush)(new SolidColorBrush(c));
            }
            catch (Exception ex)
            {
                MessageBox.Show("ToServerCommunicator: Unable to connect to server\n" + ex.Message);
            }
        }
    }
}
